package com.stylefeng.guns.modular.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitConsigneeID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.ConsigneeMapper;
import com.stylefeng.guns.modular.system.model.Consignee;
import com.stylefeng.guns.modular.system.service.IConsigneeService;

/**
 * 收货人信息控制器
 *
 * @author fengshuonan
 * @Date 2018-04-03 16:30:24
 */
@Controller
@RequestMapping("/consignee")
public class ConsigneeController extends BaseController {

    private String PREFIX = "/system/consignee/";

    @Autowired
    private IConsigneeService consigneeService;
    @Autowired
    private ConsigneeMapper consigneeMapper;

    /**
     * 跳转到收货人信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "consignee.html";
    }

    /**
     * 跳转到添加收货人信息
     */
    @RequestMapping("/consignee_add")
    public String consigneeAdd() {
        return PREFIX + "consignee_add.html";
    }

    /**
     * 跳转到修改收货人信息
     */
    @RequestMapping("/consignee_update/{consigneeId}")
    public String consigneeUpdate(@PathVariable Integer consigneeId, Model model) {
        Consignee consignee = consigneeService.selectById(consigneeId);
        model.addAttribute("item",consignee);
        LogObjectHolder.me().set(consignee);
        return PREFIX + "consignee_edit.html";
    }

    /**
     * 获取收货人信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(consigneeMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Consignee> entityWrapper = new EntityWrapper<>();
    		Wrapper<Consignee> wrapper = entityWrapper.like("consignee_name", condition);
    		page.setRecords(consigneeMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }
    
    /**
     * 通过name，模糊查询所有收货人信息
     */
    @RequestMapping(value="/listbyname")
    @ResponseBody
    public Object listByName(@RequestParam String consignee){
    	EntityWrapper<Consignee> entityWrapper = new EntityWrapper<>();
    	Wrapper<Consignee> wrapper = entityWrapper.like("consignee_name", consignee).last("limit 20");
    	return consigneeService.selectList(wrapper);
    }

    /**
     * 新增收货人信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Consignee consignee) {
    	consignee.setConsigneeId(InitConsigneeID.getInstance().initDisorderID());
        consigneeService.insert(consignee);
        return SUCCESS_TIP;
    }

    /**
     * 删除收货人信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer consigneeId) {
        consigneeService.deleteById(consigneeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改收货人信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Consignee consignee) {
        consigneeService.updateById(consignee);
        return SUCCESS_TIP;
    }

    /**
     * 收货人信息详情
     */
    @RequestMapping(value = "/detail/{consigneeId}")
    @ResponseBody
    public Object detail(@PathVariable("consigneeId") Integer consigneeId) {
        return consigneeService.selectById(consigneeId);
    }
}
